<?php
namespace app\forum\controller;
use app\base\controller\BaseController;
/**
 * 前台公共类
 */
class SiteController extends BaseController {

    protected $crumb = array();
    protected $loginUserInfo = array();

    public function __construct()
    {
        parent::__construct();
        //设置常量
        define('TPL_NAME', 'default');
        //设置用户信息
        define('USER_ID',$this->isLogin());
        if(USER_ID){
            $this->loginUserInfo = target('forum/User')->getInfo(USER_ID);
        }
        //设置路径
        $this->crumb[] = array('url'=>url('forum/Index/index'),'name'=>'首页','icon'=>'icon-home');
    }

    /**
     * 判断登录
     */
    protected function isLogin(){
        $userId = cookie('user_id');
        if (empty($userId)) {
            return 0;
        } else {
            $sign = array(
                'user_id' => $userId,
                'key' => config('SAFE_KEY'),
            );
            if(cookie('user_sign') == data_auth_sign($sign)){
                return $userId;
            }else{
                return 0;
            }
        }
    }

    /**
     * 检测论坛权限
     */
    protected function checkForumPurview($plateId,$action){
        if(!$plateId){
            $this->error('系统通讯错误！');
        }
        //获取用户组ID
        if(!USER_ID){
            $groupId = 0;
        }else{
            $groupId = $this->loginUserInfo['group_id'];
        }
        //获取板块信息
        $info = target('forum/Plate')->getInfo($plateId);
        $purview = $info['purview'];
        if(empty($purview)){
            return true;
        }
        $action = explode(',', $action);
        foreach ($action as $value) {
            if(!$purview[$value][$groupId]){
                $this->error('您所在的用户组不能进行此操作！');
            }
        }
        return true;
    }

    /**
     * 模板显示 调用内置的模板引擎
     * @access protected
     * @param string $name 模板名
     * @param bool $type 模板输出
     * @return void
     */
    protected function siteDisplay($tpl = '') {
        $common = $this->display('app/forum/view/common/frame',true);
        $foot = $this->display('app/forum/view/common/foot',true);
        if(empty($GLOBALS['tplArray'])){
            $tplArray = get_method_array($tpl);
        }else{
            $tplArray = $GLOBALS['tplArray'];
        }
        $tpl = 'app/'. strtolower($tplArray['app']) . '/view/' . strtolower($tplArray['controller']) . '/' . strtolower($tplArray['action']);
        $html = $this->display($tpl,true);
        $html = str_replace('<!--common-->', $html, $common);
        $html = str_replace('<!--bar-->', '<br>', $html);
        $html = str_replace('<!--foot-->', $foot, $html);
        echo $html;
    }

    /**
     * 错误提示方法
     */
    public function error($msg, $url = null){
        if(IS_AJAX){
            $array = array(
                'info' => $msg,
                'status' => false,
                'url' => $url,
            );
            $this->ajaxReturn($array);
        }else{
            $this->jump($msg, $url);
        }
    }

    /**
     * 成功提示方法
     */
    public function success($msg, $url = null){
        if(IS_AJAX){
            $array = array(
                'info' => $msg,
                'status' => true,
                'url' => $url,
            );
            $this->ajaxReturn($array);
        }else{
            $this->jump($msg, $url);
        }
    }

    /**
     * 页面跳转
     */
    public function jump($msg, $url = null){
        $this->assign('url',$url);
        $this->assign('msg',$msg);
        $this->display('app/forum/view/common/jump');
        exit;
    }

    /**
     * 页面Meda信息组合
     * @return array 页面信息
     */
    protected function getMedia($title='',$keywords='',$description='')
    {
        if(empty($title)){
            $title=config('site_title').' - '.config('site_subtitle');
        }else{
            $title=$title.' - '.config('site_title').' - '.config('site_subtitle');
        }
        if(empty($keywords)){
            $keywords=config('site_keywords');
        }
        if(empty($description)){
            $description=config('site_description');
        }
        return array(
            'title'=>$title,
            'keywords'=>$keywords,
            'description'=>$description,
        );
    }

    //分页结果显示
    protected function getPageShow($map = array(), $mustParams = array())
    {
        $pageArray = $this->pager;
        $html = '
        <ul class="pagination pagination-small">
          <li><a href="'.$this->createPageUrl($map,$mustParams,$pageArray['firstPage']).'">首页</a></li>
          <li><a href="'.$this->createPageUrl($map,$mustParams,$pageArray['prevPage']).'">上一页</a></li> ';
            foreach ($pageArray['allPages'] as $value) {
                if($value == 0){
                    continue;
                }
                if($value == $pageArray['page']){
                    $html .= '<li class="active">';
                }else{
                    $html .= '<li>';
                }
                $html .= '<a href="'.$this->createPageUrl($map,$mustParams,$value).'">'.$value.'</a></li> ';
           }
         $html .= '<li><a href="'.$this->createPageUrl($map,$mustParams,$pageArray['nextPage']).'">下一页</a></li>
          <li><a href="'.$this->createPageUrl($map,$mustParams,$pageArray['lastPage']).'">末页</a></li>
        </ul>';
        return $html;
    }
}
